// import {getUser} from "@/api/user.js"
const login = async ()=>{
	let userInfo =  uni.getStorageSync("userInfo")
	if(!userInfo&&!userInfo.openid){
		userInfo = await changeLogin()
	}
	return new Promise((resolve,reject)=>{
		console.log('auth',userInfo)
		resolve(userInfo)
	})
}
 const changeLogin = () => {
	// 获取服务供应商
	return new Promise(resolve=>{
		let self = this;
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				console.log(res.provider)
				let provider = res.provider[0];
				// 获取用户登录凭证code
				console.log("self.provider",provider)
				uni.login({
					provider: provider,
					success: function(loginRes) {
						console.log('uniapp统一登录凭证', loginRes)
						// 获取用户基础信息 无需授权 不含code
						console.log('userInfoP',provider)
						uni.getUserInfo({
							provider: provider,
							success(infoRes) {
								// 测试只有在微信端能拿到 支付宝端拿不到
								console.info('uniapp用户基础信息', infoRes)
								// #ifdef MP-ALIPAY
								my.getAuthCode({
									scopes: 'auth_user',
									success: (auth) => {
										console.log('支付宝授权凭证', auth)
										getUserInfo(auth.authCode).then(res=>{
											uni.setStorageSync("userInfo",res)
											resolve(res)
										}).catch(err=>{
											uni.showToast({
												title:err,
												icon:'none'
											})
										})
									
									},
								});
								// #endif
							}
						});
					}
				});
			}
		});
	})
	
}
const getUserInfo = (authCode)=>{
	return new Promise((resolve,reject)=>{
		// getUser({authCode}).then(res=>{

		// 	resolve(res.data)
		// }).catch(err=>{
		// 	reject(err.msg)
			
		// })
	})
	
}
export{
	login
}